{extend name="property/public/insidePageBase" /}
{block name="title"}房间列表{/block}
{block name="body"}
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form layui-form-pane" action="">
            <div class="layui-inline">
                <label class="layui-form-label">关键字搜索</label>
                <div class="layui-input-inline">
                    <input type="text" name="keywords" placeholder="房间名称/楼层/备注搜索" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
            </div>
        </form>

        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                {if $addAuth}<button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加房源 </button>{/if}
                {if $delAuth}<button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>{/if}
            </div>
        </script>
        <table class="layui-hide" id="propertyRoomList" lay-filter="propertyRoomList"></table>
        <script type="text/html" id="tableBar">
            <div class="layui-btn-group">
                {if $editAuth} <a class="layui-btn layui-btn-sm  layui-btn-normal" lay-event="edit">编辑</a>{/if}
                {if $copyAuth}<a class="layui-btn layui-btn-sm layui-bg-orange" lay-event="copy">复制</a>{/if}
                {if $detailAuth}<a class="layui-btn layui-btn-sm" lay-event="details">详情</a>{/if}
                {{# if(d.status === 1) { }}
                {if $delAuth}<a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</a>{/if}
                {{# } }}
            </div>
        </script>
        <script type="text/html" id="room_name_tpl">
            {{# if(d.status === 1) { }}
                <span class="layui-badge layui-bg-cyan">空</span>
            {{# } }}
            {{# if(d.status === 2) { }}
                <span class="layui-badge layui-bg-blue">租</span>
                {{# if(d.pay_rent_status == 2) { }}
                    <span class="layui-badge">欠</span>
                {{# } }}
            {{# } }}
            {{# if(d.full_room_name) { }}{{d.full_room_name}}  {{# } }}
        </script>
        <script type="text/html" id="contract_status_tpl">
            {{# if(d.contract_status === 1) { }}
                <span class="layui-badge layui-bg-orange">待审核</span>
            {{# }else if (d.contract_status === 2) { }}
                <span class="layui-badge layui-bg-blue">进行中</span>
            {{# } else { }}
                <span>---</span>
            {{# } }}

            {{# if( d.status == 2) { }}
                {{# if(d.contract_end_date) { }}{{d.contract_end_date}} 到期{{# } }}
                剩余<span style="color: red;"> {{ d.lave_days }} </span>天
            {{# } }}
        </script>
    </div>
{/block}
{block name="js"}
{__block__}
    <script>
        layui.use(['form', 'table'], function () {
            var $ = layui.jquery, form = layui.form, table = layui.table;
            var tableId = 'propertyRoomList';
            table.render({
                elem: '#'+tableId,
                id:tableId,
                url: '{:url("propertyRoomBasicList")}',
                toolbar: '#toolbar',
                cols: [[
                    {type: "checkbox", width: 50},
                    {field: 'room_name', width: 328, title: '房间名称', templet:'#room_name_tpl'},
                    {field: 'room_size', width: 110, title: '房间面积', templet:function(d) {
                            return d.room_size + ' m²';
                        },sort: true},
                    {field: 'room_rent', width: 118, title: '房间租金', templet:function(d) {
                            return d.room_rent + ' ¥/月';
                        },sort: true},
                    {field: 'room_deposit', width: 118, title: '房间押金', templet:function(d) {
                            return d.room_deposit + ' ¥';
                        },sort: true},
                    {field: 'contract_status', width: 278, title: '租赁到期时间', templet:'#contract_status_tpl', sort: true},
                    {if $editAuth || $delAuth}
                    {title: '操作', width: 298, toolbar: '#tableBar', fixed:'right'}
                    {/if}
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,
                height: 'full-80'
            });
            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) { table.reload(tableId, {page: { curr: 1  }, where: data.field}, 'data'); return false; });
            /**
             * toolbar监听事件
             */
            table.on('toolbar('+tableId+')', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var url = '{:url("propertyRoomBasicAdd")}';
                    var index = layer.open({title: '添加房屋', type: 2, shade: 0.2,maxmin:true, area: ['650px', '80%'], content: url, btnAlign: 'c', btn: ['确定', '取消'], yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index], submit = layero.find('iframe').contents().find("#submitBtn");
                            iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                                var field = data.field; //获取提交的字段
                                var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                                $.post(url, field, function (res) {
                                    layer.close(loadAdd);
                                    if (res.code === 0) {
                                        table.reload(tableId); //数据刷新
                                        layer.close(index); //关闭弹层
                                        layer.msg(res.msg, {icon: 6, offset: '15px'});
                                    } else {
                                        layer.msg(res.msg, {icon: 5, offset: '15px'});
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'delete') {  // 监听删除操作
                    var checkStatus = table.checkStatus(obj.config.id),data = checkStatus.data;
                    var data = checkStatus.data, id = [];
                    for (elem in data) {
                        id.push(data[elem].id);
                    }
                    if (id.length === 0) {
                        return layer.msg('请选择要删除的数据');
                    }
                    var url = '{:url("propertyRoomBasicDel")}';
                    layer.confirm('出租中、预约 、预定的房间过滤删除！  是否确认? ', {icon: 3}, function () {
                        var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                        $.post(url, {ids: id}, function (res) {
                            layer.close(loadAdd);
                            if (res.code === 0) {
                                table.reload(tableId); //数据刷新
                                layer.msg(res.msg, {icon: 6, offset: '15px'});
                            } else {
                                layer.msg(res.msg, {icon: 5, offset: '15px'});
                            }
                        });
                    });
                }
            });

            //监听表格复选框选择
            table.on('checkbox('+tableId+')', function (obj) {
                console.log(obj)
            });
            table.on('tool('+tableId+')', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {
                    var url = '{:url("propertyRoomBasicEdit")}?id='+data.id;
                    var index = layer.open({
                        title: '编辑', type: 2,  shade: 0.2, maxmin:true, area:['650px', '80%'],content: url, btnAlign: 'c', btn: ['确定', '取消'], yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index], submit = layero.find('iframe').contents().find("#submitBtn");
                            iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                                var field = data.field; //获取提交的字段
                                var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                                $.post(url, field, function (res) {
                                    layer.close(loadAdd);
                                    if (res.code === 0) {
                                        table.reload(tableId); //数据刷新
                                        layer.close(index); //关闭弹层
                                        layer.msg(res.msg, {icon: 6, offset: '15px'});
                                    } else {
                                        layer.msg(res.msg, {icon: 5, offset: '15px'});
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'copy') {
                    var url = '{:url("propertyRoomBasicCopy")}?id='+data.id;
                    var index = layer.open({
                        title: '复制', type: 2,  shade: 0.2, maxmin:true, area:['620px', '80%'],content: url, btnAlign: 'c', btn: ['确定', '取消'], yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index], submit = layero.find('iframe').contents().find("#submitBtn");
                            iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                                var field = data.field; //获取提交的字段
                                var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                                $.post(url, field, function (res) {
                                    layer.close(loadAdd);
                                    if (res.code === 0) {
                                        table.reload(tableId); //数据刷新
                                        layer.close(index); //关闭弹层
                                        layer.msg(res.msg, {icon: 6, offset: '15px'});
                                    } else {
                                        layer.msg(res.msg, {icon: 5, offset: '15px'});
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'details') {
                    var url = '{:url("propertyRoomBasicDetail")}?room_id=' + data.id;
                    var index = parent.layer.open({title: data.room_name + ' 详情 ',  type: 2, shade: 0.2,maxmin: true, shadeClose: true,   move: false, area: ['1100px', '100%'], offset: 'rt',  anim: 2,  content: url,  btn: false  });
                    return false;
                } else if (obj.event === 'delete') {
                    var url = '{:url("propertyRoomBasicDel")}';
                    layer.confirm('删除后不可恢复！  是否确认? ', {icon: 3}, function () {
                        var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                        $.post(url, {ids: [data.id]}, function (res) {
                            layer.close(loadAdd);
                            if (res.code === 0) {
                                table.reload(tableId); //数据刷新
                                layer.msg(res.msg, {icon: 6, offset: '15px'});
                            } else {
                                layer.msg(res.msg, {icon: 5, offset: '15px'});
                            }
                        });
                    });
                }
            });
        });
    </script>
{/block}